package com.martini.satoken01.utils;

import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.engine.velocity.VelocityEngine;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * @author martini at 2021/12/19 14:24
 */
public class VelocityTemplateUtils implements TemplateUtils {
    @Override
    public String renderTemplate(String path, String templateName, Map<String, Object> data) {
        // 配置模板引擎
        VelocityEngine ve = new VelocityEngine();
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setCharset(Charset.defaultCharset());
        templateConfig.setPath(path);
        templateConfig.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH);
        ve.init(templateConfig);

        // 生成渲染后的数据
        return ve.getTemplate(templateName).render(data);
    }
}
